package com.fw.web.controller.system;

import java.util.HashMap;
import java.util.List;

import com.fw.system.admin.domain.TbUser;
import com.fw.system.admin.service.ITbUserService;
import com.google.common.collect.Maps;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fw.annotation.Log;
import com.fw.core.controller.BaseController;
import com.fw.core.domain.AjaxResult;
import com.fw.enums.BusinessType;
import com.fw.system.admin.domain.TbAmountLog;
import com.fw.system.admin.service.ITbAmountLogService;
import com.fw.utils.poi.ExcelUtil;
import com.fw.core.page.TableDataInfo;

/**
 * 支出收益Controller
 * 
 * @author xxx
 * @date 2021-12-03
 */
@RestController
@RequestMapping("/admin/amount_log")
public class TbAmountLogController extends BaseController
{
    @Autowired
    private ITbAmountLogService tbAmountLogService;
    @Autowired
    private ITbUserService userService;

    /**
     * 查询支出收益列表
     */
    @PreAuthorize("@ss.hasPermi('admin:amount_log:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbAmountLog tbAmountLog)
    {
        HashMap<String, TbUser> newHashMap = Maps.newHashMap();
        startPage();
        List<TbAmountLog> list = tbAmountLogService.selectTbAmountLogList(tbAmountLog);
        for (TbAmountLog amountLog : list) {
            if (amountLog.getLogType().equals(2L)){
                // 提现
                TbUser tbUser = null;
                if ( (tbUser =newHashMap.get(amountLog.getUserId())) != null){
                    // 用 map
                }else{
                    tbUser = userService.getById(amountLog.getUserId());
                    newHashMap.put(amountLog.getUserId(),tbUser);
                }
                amountLog.setAliAccount(tbUser.getAliAccount());
                amountLog.setAliUserName(tbUser.getAliUserName());
                amountLog.setBankCar(tbUser.getBankCar());
                amountLog.setBankName(tbUser.getBankName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出支出收益列表
     */
    @PreAuthorize("@ss.hasPermi('admin:amount_log:export')")
    @Log(title = "支出收益", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbAmountLog tbAmountLog)
    {
        List<TbAmountLog> list = tbAmountLogService.selectTbAmountLogList(tbAmountLog);
        ExcelUtil<TbAmountLog> util = new ExcelUtil<TbAmountLog>(TbAmountLog.class);
        return util.exportExcel(list, "amount_log");
    }

    /**
     * 获取支出收益详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:amount_log:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(tbAmountLogService.selectTbAmountLogById(id));
    }

    /**
     * 新增支出收益
     */
    @PreAuthorize("@ss.hasPermi('admin:amount_log:add')")
    @Log(title = "支出收益", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbAmountLog tbAmountLog)
    {
        return toAjax(tbAmountLogService.insertTbAmountLog(tbAmountLog));
    }

    /**
     * 修改支出收益
     */
    @PreAuthorize("@ss.hasPermi('admin:amount_log:edit')")
    @Log(title = "支出收益", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbAmountLog tbAmountLog)
    {
        return toAjax(tbAmountLogService.updateTbAmountLog(tbAmountLog));
    }

    /**
     * 删除支出收益
     */
    @PreAuthorize("@ss.hasPermi('admin:amount_log:remove')")
    @Log(title = "支出收益", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tbAmountLogService.deleteTbAmountLogByIds(ids));
    }
}
